package com.fairytask.ui.effect
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.data.Modulator;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.engine.resource.QueueLoader;
	import com.jzonl.event.EffectEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;

	public class PackageEffect extends Sprite
	{
		private var mc			:Bitmap;
		private var res		:String;
		private var itemSpace	:Sprite;
		private var model		:Modulator	=	Modulator.instance;
		
		public function PackageEffect(pName:String,resPath:String)
		{
			super();
			res=ResConfig.iconPath+resPath;
			//			res="res/png/icon/13019001.png";
			loadRes(res);
		}
		
		private function loadRes(res:String):void
		{
			QueueLoader.StartLoad(res,onLoad);			
		}
		
		private function onLoad(val:Bitmap):void
		{
			mc=val;
			mc.smoothing=true;
			init();
		}
		
		
		private function init():void
		{
			itemSpace = new Sprite();
			itemSpace.x=420;
			itemSpace.y=200;
			itemSpace.addChild(mc);
			addChild(itemSpace);
			itemSpace.alpha = 0;
			TweenMax.to(itemSpace,0.1,{alpha:1,y:180,onComplete:onAlpha});
		}
		
		/**
		 * 播放运动动画 
		 * 
		 */
		private function onAlpha():void
		{
			//弧线锚定位置				终点位置			
			TweenMax.to(itemSpace,2,{alpha:0.5,bezierThrough:[{x:518, y:228},{x:640 + GameStage.mainLayer.x, y:550 + GameStage.mainLayer.y}],scaleX:0.4,scaleY:0.4,ease:Cubic.easeOut,onComplete:onComplete});			
		}
		
		/**
		 * 移动结束后，慢慢消失 
		 * 
		 */
		/*private function onMoveOver():void
		{
			TweenMax.to(itemSpace,1,{alpha:0,onComplete:onComplete});
		}*/
		
		/**
		 * 动画完成 
		 * 
		 */
		private function onComplete():void
		{
			this.removeChild(itemSpace);
			model.dispatchEvent(new EffectEvent(EffectEvent.PACKAGE_COMPLETE));
		}
		
		public function show():void
		{
			GameStage.uiSpace.addTop(this);
		}
		
		public function hide():void
		{
			if(null != this.parent)
			{
				this.parent.removeChild(this);
			}
		}
	}
}